import config from './config'
export function askQuestion (msgList, signal) {
    const lastMsg = msgList[msgList.length - 1]
    let arr = msgList
    if (lastMsg.role == 'system' && lastMsg.content == '') {
        arr = msgList.slice(0, -1)
    }
    const payload = {
        messages: arr.map(item =>{
            return {
                role: item.role,
                content: item.content
            }
        }),
        model: config.model,
        stream: true,
        max_tokens: config.maxTokens,
        temperature: config.temperature,
        top_k: config.topK,
        top_p: config.topP,
        frequency_penalty: config.frequencyPenalty
    }
    return fetch(config.baseUrl, {
        signal,
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'authorization': 'Bearer ' + config.apiKey
        },
        mode: 'cors',
        body: JSON.stringify(payload)
    })
}
